<?php
class CommentController extends Controller
{
	public function actionPost()
	{
		if(!isset($_GET['tid']))
			throw new CHttpException(404,'invalid request');

		$tid=(int)$_GET['tid'];
		$topic=Topic::model()->findByPk($tid);

		//TODO 验证是否属于这个小组

		if(empty($topic))
			throw new CHttpException(404,'invalid request');

		$comment=new Comment;

		if(isset($_POST['Comment']))
		{
			$comment->attributes=$_POST['Comment'];
			$comment->rela_type=ENTITY_TYPE_TOPIC;
			$comment->rela_id=$tid;
			$comment->uid=Yii::app()->user->id;

			if($comment->save())
				$this->redirect($this->createUrl('topic/index',array('id'=>$topic->id)).'#comments');
			else
				throw new CHttpException(404,'invalid request');
		}
	
	}

	public function actionDelete()
	{
		if(!isset($_GET['id']))
			throw new CHttpException(404,'invalid request');
		
		$comment=Comment::model()->findByPk($_GET['id']);
		if(empty($comment))
			throw new CHttpException(404,'invalid request');

		//TODO 权限验证
		if(Yii::app()->user->isGuest || Yii::app()->user->id != $comment->uid)
			return;

		$tid=$comment->rela_id;
		if($comment->delete())
			$this->redirect($this->createUrl('topic/index',array('id'=>$tid)));
	}
}
?>
